package handler

import (
	"net/http"

	"gitee.com/thewalkers/refactoring-code/model"
	"gitee.com/thewalkers/refactoring-code/model/apperrors"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type addHourlyEmployeeParams struct {
	Name    string  `json:"employee_name" binding:"required"`
	Address string  `json:"employee_address" binding:"required"`
	Rate    float64 `json:"rate" binding:"required"`
}

func (h *Handler) AddHourlyEmployee(ctx *gin.Context) {
	var req addHourlyEmployeeParams
	// Bind incoming json to struct and check for validation errors
	if ok := bindData(ctx, &req); !ok {
		return
	}

	e := &model.Employee{
		Name:    req.Name,
		Address: req.Address,
	}

	ho := &model.HourClassification{
		Rate: req.Rate,
	}

	err := h.AddHourlyEmployeeTransactionService.Create(ctx, e, ho)
	if err != nil {
		zap.L().Error("service failed to add hourly employee transaction", zap.Error(err))
		ctx.JSON(apperrors.Status(err), gin.H{
			"error": err,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"msg": createEmployeeSuccess,
	})
}
